package com.fengyuncx.tp.flutter.bugly_plugin;

import android.content.Context;

import androidx.annotation.NonNull;

import com.tencent.bugly.Bugly;
import com.tencent.bugly.beta.Beta;
import com.tencent.bugly.crashreport.CrashReport;

import java.util.Map;

import io.flutter.BuildConfig;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugin.common.PluginRegistry.Registrar;

/** BuglyPlugin */
public class BuglyPlugin implements FlutterPlugin, MethodCallHandler {
  /// The MethodChannel that will the communication between Flutter and native Android
  ///
  /// This local reference serves to register the plugin with the Flutter Engine and unregister it
  /// when the Flutter Engine is detached from the Activity
  private MethodChannel channel;

  public static void init(Context context, String appId, boolean debug, boolean isUploadProcess) {
    CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(context);
    strategy.setUploadProcess(isUploadProcess);
    CrashReport.setIsDevelopmentDevice(context, BuildConfig.DEBUG);
//    CrashReport.initCrashReport(context, appId, debug, strategy);
    //升级检查周期设置
    Beta.upgradeCheckPeriod = 60 * 1000;
    Bugly.init(context, appId, debug, strategy);
  }

  @Override
  public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
    channel = new MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "bugly_plugin");
    channel.setMethodCallHandler(this);
  }

  // This static function is optional and equivalent to onAttachedToEngine. It supports the old
  // pre-Flutter-1.12 Android projects. You are encouraged to continue supporting
  // plugin registration via this function while apps migrate to use the new Android APIs
  // post-flutter-1.12 via https://flutter.dev/go/android-project-migration.
  //
  // It is encouraged to share logic between onAttachedToEngine and registerWith to keep
  // them functionally equivalent. Only one of onAttachedToEngine or registerWith will be called
  // depending on the user's project. onAttachedToEngine or registerWith must both be defined
  // in the same class.
  public static void registerWith(Registrar registrar) {
    final MethodChannel channel = new MethodChannel(registrar.messenger(), "bugly_plugin");
    channel.setMethodCallHandler(new BuglyPlugin());
  }

  @Override
  public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
    if (call.method.equals("getPlatformVersion")) {
      result.success("Android " + android.os.Build.VERSION.RELEASE);
    } else if(call.method.equals("postDartError")) {
      Map<String, String> exceptionMap = (Map) call.arguments;
      CrashReport.postException(4, "Dart Exception", exceptionMap.get("message"), exceptionMap.get("stack"), null);
    } else if(call.method.equals("postTestError")) {
      CrashReport.testJavaCrash();
    } else if(call.method.equals("postANRError")) {
      CrashReport.testANRCrash();
    } else if(call.method.equals("postNativeError")) {
      CrashReport.testNativeCrash();
    } else if(call.method.equals("checkUpdate")){
      Beta.checkUpgrade();
    } else {
      result.notImplemented();
    }
  }

  @Override
  public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
    channel.setMethodCallHandler(null);
  }
}
